home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9348 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.6 KB

  1. Path: news.lpr.carel.fi!usenet
  2. From: Ari Lukumies <aril@cmt.lpr.mail.carel.fi>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Input into array of strings?
  5. Date: Fri, 08 Mar 1996 14:04:19 +0200
  6. Organization: Carelcomp Forest
  7. Message-ID: <31402243.A48@cmt.lpr.mail.carel.fi>
  8. References: <4hnv1e$o7n@rhea.glo.be>
  9. NNTP-Posting-Host: renoir.cclahti.carel.fi
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (WinNT; I)
  14.  
  15. Sven Claeys wrote:
  16. > Hi,
  17. > I've got a some kind of problem reading into arrays of strings. I'm a
  18. > bit rusty in C or C++, it's been a while, so please be patient with
  19. > me.
  20. > here we go:
  21. > i've got a structure
  22. > struct ImmoData
  23. > {
  24. >    char code[3][5];
  25. >    ....
  26. > }....;
  27. > now i want to read data from a file with fgets(...) into this array.
  28. > I've tried
  29. >    for (i=0; i<5; i++)
  30. >        fgets(code[i],....);
  31. > or
  32. >    for (i=0; i<5; i++)
  33. >        fgets((char*) code[i], ...);
  34. > but none of these seem to work. As i'm debugging the first code is
  35. > read correct, but the second is appended to the first and is also read
  36. > into the second, and so on.
  37.  
  38. You have declared a structure type that has member 'code' having three strings, their 
  39. length being 5 bytes. If you really wanted to get five strings, then:
  40.  
  41.     struct ImmoData {
  42.         char    code[5][5];    /* five strings upto 5 bytes length */
  43.     };
  44.  
  45. would be a better solution. Also, your fgets(code ...) doesn't reference the member 
  46. inside the struct. Try declaring a variable, eg:
  47.  
  48.     struct ImmoData    var;
  49.     ...
  50.     fgets(var.code[i], ...);
  51.  
  52. HTH,
  53.  AriL
  54. -- 
  55. All my opinions are mine and mine alone.
  56.